package chapter5.lifecycle;

import org.springframework.beans.factory.BeanCreationException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.GenericXmlApplicationContext;

public class TestSimpleBean {
	private static SimpleBean getBean(String beanName, ApplicationContext ctx) {
		try {
			SimpleBean bean = (SimpleBean) ctx.getBean(beanName);
			System.out.println(beanName);
			return bean;
		} catch (BeanCreationException e) {
			System.out.println("An error occured in bean configuration: "
					+ e.getMessage());
			return null;
		}
	}

	public static void main(String[] args) {
		GenericXmlApplicationContext ctx = new GenericXmlApplicationContext();
		ctx.load("classpath:chapter5/lifecycle/initMethod.xml");
		ctx.refresh();

		SimpleBean bean1 = getBean("simpleBean1", ctx);
		System.out.println(bean1.toString());
		SimpleBean bean2 = getBean("simpleBean2", ctx);
		System.out.println(bean2.toString());
		SimpleBean bean3 = getBean("simpleBean3", ctx);
		System.out.println(bean3.toString());
	}
}
